package com.cans.strategy;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * 使用策略模式进行优化
 *
 * @author cans
 * @date 2022/3/26
 **/
public class Calculator2 {

    private  Map<String,CalculatorOperator> calculatorOperatorMap = new HashMap<>();

    public Calculator2(){
        CalculatorOperatorAddImpl calculatorOperatorAdd = new CalculatorOperatorAddImpl();
        CalculatorOperatorSubImpl calculatorOperatorSub = new CalculatorOperatorSubImpl();
        calculatorOperatorMap.put(calculatorOperatorAdd.getOperator(),calculatorOperatorAdd);
        calculatorOperatorMap.put(calculatorOperatorSub.getOperator(),calculatorOperatorSub);
    }

    /**
     * 计算数字
     *
     * @param num1     参数1
     * @param num2     参数2
     * @param operator 操作算法
     * @return
     */
    public Integer calculate(Integer num1, Integer num2, String operator) {
        CalculatorOperator calculatorOperator = calculatorOperatorMap.get(operator);
        if(calculatorOperator == null){
            throw new RuntimeException("无效的操作算法:" + operator);
        }
        return calculatorOperator.calculate(num1, num2);
    }

    /**
     * 支持的算法枚举
     */
    public enum Operator{
        ADD("+"),SUB("-");

        private String operator;

        Operator(String operator){
            this.operator = operator;
        }

        public String getOperator() {
            return operator;
        }

        public void setOperator(String operator) {
            this.operator = operator;
        }
    }

    /**
     * 计算顶层接口
     */
    public interface CalculatorOperator{
        /**
         * 算法操作
         * @return
         */
        public String getOperator();

        /**
         * 计算方法
         *
         * @param num1 参数1
         * @param num2 参数2
         * @return
         */
        public Integer calculate(Integer num1, Integer num2);
    }

    /**
     * 加法实现
     */
    public class CalculatorOperatorAddImpl implements CalculatorOperator{

        @Override
        public String getOperator() {
            return Operator.ADD.getOperator();
        }

        /**
         * 加法操作
         * @param num1 参数1
         * @param num2 参数2
         * @return
         */
        public Integer calculate(Integer num1, Integer num2) {
         return num1 + num2;
        }
    }

    /**
     * 减法实现
     */
    public class CalculatorOperatorSubImpl implements CalculatorOperator{

        @Override
        public String getOperator() {
            return Operator.SUB.getOperator();
        }

        /**
         * 加法操作
         * @param num1 参数1
         * @param num2 参数2
         * @return
         */
        public Integer calculate(Integer num1, Integer num2) {
            return num1 - num2;
        }
    }

    @Test
    public void testCalculate(){
        System.out.println("1+1 = : " + calculate(1,1,"+"));
        System.out.println("1-1 = : " + calculate(1,1,"-"));
        System.out.println("1*1 = : " + calculate(1,1,"*"));
    }
}
